﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;

namespace LeSoft.Application.Web.Areas.QualificationManage.Controllers
{
    public class SelectRoomRecordController : MvcControllerBase
    {
        //
        // GET: /QualificationManage/SelectRoomRecord/
        SelectRoomRecordBLL bll = new SelectRoomRecordBLL();
        #region 视图
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.Visit, "选房首页", "Index")]
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.Visit, "选房详情", "Form")]
        public ActionResult Form()
        {
            return View();
        }
        #endregion

        #region 查询数据

        /// <summary>
        /// 创建人：hxf
        /// 日  期：2017-10-11
        /// 描  述：查询选房记录
        /// </summary>
        /// <param name="condition">查询条件（会员ID或者房间ID）</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Ignore)]
        [HandlerAction(OperationType.GetList, "选房记录", "GetList")]
        public ActionResult GetList(string condition)
        {
            return Content(bll.GetList(condition).ToJson());
        }

        /// <summary>
        /// 创建人：hxf
        /// 日  期：2017-10-11
        /// 描  述：分页查询选房记录
        /// </summary>
        /// <param name="pagination">分页</param>
        /// <param name="queryJson">查询条件（State,OrganizeId,PstructId,BeginDate,EndDate,Keyword）</param>
        /// <returns></returns> 
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList, "数据列表", "GetPageList")]
        public ActionResult GetPageList(Pagination pagination, string queryJson)
        {
            var watch = CommonHelper.TimerStart();
            if (!string.IsNullOrEmpty(queryJson))
            {
                var queryParam = queryJson.ToJObject();
                if (!queryParam["OrganizeId"].IsEmpty())
                {
                    string OrganizeId = new Cache.PStructCache().GetSonOrganizeId(queryParam["OrganizeId"].ToString());
                    queryParam["OrganizeId"] = OrganizeId;
                }
                if (!queryParam["PstructId"].IsEmpty())
                {
                    string PstructId = new Cache.PStructCache().GetSonStructId(queryParam["PstructId"].ToString());
                    queryParam["PstructId"] = PstructId;
                }
                queryJson = queryParam.ToJson();
            }
            var data = bll.GetPageList(pagination, queryJson);
            var JsonData = new
            {
                rows = data,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }


        /// <summary>
        /// 创建人：hxf
        /// 日  期：2017-10-11
        /// 描  述：获取实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        [HandlerAction(OperationType.GetList, "单据详情", "GetEntity")]
        public ActionResult GetEntity(string keyValue)
        {
            try
            {
                var selectRoomRecordEntity = bll.GetEntity(keyValue);
                var contractEntity = new ContractDWellingBLL().GetContractDWellingEntity(selectRoomRecordEntity.ContractId);
                var roomEntity = new RoomBLL().GetEntity("", selectRoomRecordEntity.RoomId);
                var pstructEntity = new PStructBLL().GetEntity(roomEntity.PStructId);
                var memberEntity = new MemberBLL().GetEntity(selectRoomRecordEntity.MemberId);
                var obj = new { selectRoomRecordEntity = selectRoomRecordEntity, roomEntity = roomEntity, contractEntity = contractEntity, pstructEntity = pstructEntity, memberEntity = memberEntity };
                return Content(obj.ToJson());
            }
            catch (Exception e)
            {
                return Error("数据查询出错;" + e.Message);
            }
        }
        #endregion

        #region 保存数据
        /// <summary>
        /// 创建人：hxf
        /// 日  期：2017-10-11
        /// 描  述：保存选房记录
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="selectRoomRecordEntity">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "保存选房记录", "SaveForm")]
        public ActionResult SaveForm(string keyValue, SelectRoomRecordEntity selectRoomRecordEntity)
        {
            try
            {
                if (string.IsNullOrEmpty(keyValue))
                {
                    var model = new RoomBLL().GetEntity("PStructId", selectRoomRecordEntity.RoomId);
                    if (model.State > 1) return Error("只能选择待租房或准备房");
                    selectRoomRecordEntity.RoomId = model.Id;
                }
                bll.SaveForm(keyValue, selectRoomRecordEntity, "ISBack");
                return Success("操作成功！");
            }
            catch (Exception e)
            {
                return Error(e.Message);
                throw;
            }
        }
        /// <summary>
        /// Careat:yzp
        /// Date:2018-03-37
        /// Memo:后台取消选房
        /// </summary>
        /// <param name="keyValue"></param>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        [HandlerAction(OperationType.Create, "取消选房记录", "SaveForm")]
        public ActionResult canceForm(string keyValue)
        {
            try
            {
                bll.canceForm(keyValue);
                return Success("操作成功！");
            }
            catch (Exception e)
            {
                return Error("取消失败：" + e.Message);
                throw;
            }
        }

        #endregion
    }
}
